package edu.chinasoft.spring03;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.cglib.proxy.MethodInterceptor;
import java.lang.reflect.Method;
public
class CompanyFactory implements MethodInterceptor {
    private XiaoMing xiaoMing;    //目标类
    private XiaoWu xiaoWu;

    public
    CompanyFactory() {
    }

    public
    CompanyFactory(XiaoMing xiaoMing, XiaoWu xiaoWu) {
        this.xiaoMing = xiaoMing;
        this.xiaoWu = xiaoWu;
    }
    //创建代理类

    public XiaoMing
    creatProxy( ) {
        //通过cglib组件去创建
        //创建创建代理类的工具类
        Enhancer enhancer = new Enhancer();
        //设置代理类的父类
        enhancer.setSuperclass( xiaoMing.getClass() );
        enhancer.setCallback(this);
        //设置代理类如何去扩展目标类的功能
        XiaoMing proxy = (XiaoMing) enhancer.create();
        //创建并返回代理对象
        return proxy;
    }
    //设置代理类如何去扩展目标类

    @Override
    public
    Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        xiaoWu.start();
        Object result=method.invoke(xiaoMing,objects);
        xiaoWu.end();
        return result;
    }
}
